//
//  kjDetailsViewController.m
//  gpos_app
//
//  Created by apple on 2018/8/20.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "kjDetailsViewController.h"
#import "Masonry.h"
#import "GPosAPI.h"
#import "XMRegisterViewController.h"
#import "SVProgressHUD.h"
@interface kjDetailsViewController ()
@property (nonatomic,strong)UIView * topView;
@property (nonatomic,strong)UILabel *TimeLabel;
@property (nonatomic,strong)NSString * timeStr;

@property (nonatomic,strong)NSString  *otherProfit;
@property (nonatomic,strong)NSString * ownProfit;
@property (nonatomic,strong)NSString * totalProfit;

@end

@implementation kjDetailsViewController

//改变状态栏
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:244/255.0f green:244/255.0f blue:244/255.0f alpha:1];
    self.title = @"账单详情";
    [self loadData];
    NSLog(@"---%@",self.tid);
}

- (void)loadData{
    int user = [[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"] intValue];
    NSDictionary * params = @{@"userId" : @(user),
                              @"id" : @([self.tid intValue])
                              };
    
    [GPosAPI requestGetUserMinerDetailWithParams:params andCallback:^(id obj) {
        NSLog(@"0---%@",obj);
        if (obj == nil) {
            [SVProgressHUD showInfoWithStatus:@"请求失败"];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
            [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
            
        }else {
            
            if ([[obj objectForKey:@"code"] intValue] == 200) {
                if ([obj objectForKey:@"data"] != nil ) {
                    
//                    float tota = [[[obj objectForKey:@"data"] objectForKey:@"totalProfit"] floatValue];
//                    NSNumber * nsNumber1 = @(tota);
                    NSString * str =[[obj objectForKey:@"data"] objectForKey:@"totalProfit"];
                    self.totalProfit = [NSString stringWithFormat:@"%@",@(str.floatValue)];
//                    NSNumber * nsNumber2 = @([[[obj objectForKey:@"data"] objectForKey:@"otherProfit"] floatValue]);
                    NSString * number = [[obj objectForKey:@"data"] objectForKey:@"otherProfit"];
                    self.otherProfit = [NSString stringWithFormat:@"+%@",@(number.floatValue)];
//                     NSNumber * nsNumber3 = @([[[obj objectForKey:@"data"] objectForKey:@"ownProfit"] floatValue]);
                    NSString * ower = [[obj objectForKey:@"data"] objectForKey:@"ownProfit"];
                    self.ownProfit = [NSString stringWithFormat:@"+%@",@(ower.floatValue)];
                }
                
            }else if ([[obj objectForKey:@"code"] intValue] == 401 || [[obj objectForKey:@"code"] intValue] == 402) {
                
                UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
                //storyboard里的ID
                XMRegisterViewController *vc = [mainStory instantiateInitialViewController];
                vc.navigationController.navigationBarHidden=NO;
                //                    //设置当前窗口的根控制器
                
                [UIApplication sharedApplication].keyWindow.rootViewController=vc;
                [SVProgressHUD showInfoWithStatus:[obj objectForKey:@"message"]];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                
                
                
            }else {
                [SVProgressHUD showErrorWithStatus:[obj objectForKey:@"message"]];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                
            }
            
        }
        [self topLoadView];
    }];
    
}
- (void)topLoadView {
    self.topView = [[UIView alloc] init];
    self.topView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(kNavbarHeight);
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
        make.height.equalTo(@30);
    }];
    
    UILabel * leftLabel = [[UILabel alloc] init];
    leftLabel.font = [UIFont systemFontOfSize:12];
    leftLabel.textColor = [UIColor grayColor];
    [self.topView addSubview:leftLabel];
    [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.topView);
        make.right.equalTo(self.topView).with.offset(-16);
    }];
    
    self.TimeLabel = [[UILabel alloc] init];
    self.TimeLabel.font = [UIFont systemFontOfSize:12];
    self.TimeLabel.textColor = [UIColor lightGrayColor];
    [self.topView addSubview:self.TimeLabel];
    [self.TimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.topView);
        make.left.equalTo(self.topView).with.offset(16);
        
    }];
    // timeStampString 是服务器返回的13位时间戳
    NSString *timeStampString  = self.timeDate;
    
    // iOS 生成的时间戳是10位
    NSTimeInterval interval=[timeStampString doubleValue] / 1000.0;
    NSDate *date  = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateString    = [formatter stringFromDate: date];
    self.timeStr =dateString;
//    leftLabel.text = @"第一阶段";
    self.TimeLabel.text = [NSString stringWithFormat:@"%@(单位:GPOS)",self.timeStr];
    
    UIView * kjsyBGView = [[UIView alloc] init];
    kjsyBGView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:kjsyBGView];
    [kjsyBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom).with.offset(0);
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
        make.height.equalTo(@180);
    }];
    
    UIImageView * kjImageView = [[UIImageView alloc] init];
    kjImageView.image = [UIImage imageNamed:@"icon_kjsy"];
    [kjsyBGView addSubview:kjImageView];
    [kjImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(kjsyBGView).with.offset(30);
        make.centerX.equalTo(kjsyBGView);
        make.height.equalTo(@47);
    }];
    
    UILabel * kjsytishiLabel = [[UILabel alloc] init];
    kjsytishiLabel.font = [UIFont systemFontOfSize:14];
    kjsytishiLabel.textColor = [UIColor blackColor];
    [kjsyBGView addSubview:kjsytishiLabel];
    [kjsytishiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(kjImageView.mas_bottom).with.offset(10);
        make.centerX.equalTo(kjsyBGView);
    }];
    
    
    UILabel * kjzshouyiLabel = [[UILabel alloc] init];
    kjzshouyiLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
    kjzshouyiLabel.textColor = [UIColor colorWithRed:70/255.0f green:85/255.0f blue:254/255.0f alpha:1];
    [kjsyBGView addSubview:kjzshouyiLabel];
    [kjzshouyiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(kjsytishiLabel.mas_bottom).with.offset(14);
        make.centerX.equalTo(kjsyBGView);
    }];
    kjsytishiLabel.text = @"GPOS智能总收益";
    kjzshouyiLabel.text = self.totalProfit;
    
    UIView * bottomBGView = [[UIView alloc] init];
    bottomBGView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomBGView];
    [bottomBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(kjsyBGView.mas_bottom).with.offset(10);
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
        make.height.equalTo(@109);
    }];
    
    UIView * bottomSXView = [[UIView alloc] init];
    bottomSXView.backgroundColor = [UIColor grayColor];
    [bottomBGView addSubview:bottomSXView];
    [bottomSXView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bottomBGView).with.offset(10);
        make.right.equalTo(bottomBGView).with.offset(-10);
        make.centerY.equalTo(bottomBGView);
        make.height.equalTo(@0.5);
    }];
    
    UILabel * bottomkjsyts = [[UILabel alloc] init];
    bottomkjsyts.font = [UIFont systemFontOfSize:14];
    bottomkjsyts.textColor = [UIColor grayColor];
    [bottomBGView addSubview:bottomkjsyts];
    [bottomkjsyts mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bottomBGView).with.offset(5);
        make.left.equalTo(bottomBGView).with.offset(15);
        make.bottom.equalTo(bottomSXView.mas_top).with.offset(-5);
    }];
    
    UILabel * bottomkjsyje  = [[UILabel alloc] init];
    bottomkjsyje.font = [UIFont systemFontOfSize:14];
    bottomkjsyje.textColor = [UIColor blackColor];
    [bottomBGView addSubview:bottomkjsyje];
    [bottomkjsyje mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bottomBGView).with.offset(5);
        make.right.equalTo(bottomBGView).with.offset(-15);
        make.bottom.equalTo(bottomSXView.mas_top).with.offset(-5);
    }];
    
    UILabel * bottomtjsyts = [[UILabel alloc] init];
    bottomtjsyts.font = [UIFont systemFontOfSize:14];
    bottomtjsyts.textColor = [UIColor grayColor];
    [bottomBGView addSubview:bottomtjsyts];
    [bottomtjsyts mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bottomSXView.mas_bottom).with.offset(5);
        make.left.equalTo(bottomBGView).with.offset(15);
        make.bottom.equalTo(bottomBGView).with.offset(-5);
    }];
    
    UILabel * bottomtjsyje  = [[UILabel alloc] init];
    bottomtjsyje.font = [UIFont systemFontOfSize:14];
    bottomtjsyje.textColor = [UIColor blackColor];
    [bottomBGView addSubview:bottomtjsyje];
    [bottomtjsyje mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bottomSXView.mas_bottom).with.offset(5);
        make.right.equalTo(bottomBGView).with.offset(-15);
        make.bottom.equalTo(bottomBGView).with.offset(-5);
    }];
    
    bottomkjsyts.text = @"GPOS智能收益";
    bottomtjsyts.text = @"推荐收益";
    bottomkjsyje.text = self.ownProfit;
    bottomtjsyje.text = self.otherProfit;
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
